<template>
	<view>
		<uni-nav-bar :fixed="true" @clickLeft="$pageBack()" left-icon="back" title="提现" background-color="#ECE6D5" :status-bar="true" border="false"></uni-nav-bar>
		<view class="top_bcg">
			<view class="">提现至 微信账户</view>
		</view>
		<view class="content">
			<view class="tit">提现金额</view>
			<view class="inputs flex_jc_sb">
				<view class="flex">
					<view class="i_l">￥</view>
					<input v-model="params.value" type="digit" auto-focus="true" placeholder-class="input_text" placeholder="请输入" />
				</view>
				<view class="tt" @click="allvalue">全部提现</view>
			</view>
			<view class="tts">当前可提现余额{{ amount }}元</view>
		</view>
		<view class="btn" @click="open">申请提现</view>
		<view @click="$pageGo('/mypage/more/withdrawRules')" class="" style="font-size: 28rpx;color: #75613E;text-align: center;">
			提现规则 
			<uni-icons size="15" type="right" color="#75613E"></uni-icons>
		</view>

		<uni-popup ref="popupRef" type="center">
			<view class="popup">
				<view class="pop_content">{{ ts }}</view>
				<view class="pop_btn" @click="close">我知道了</view>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
import { ref, reactive, getCurrentInstance } from 'vue';
import { onLoad, onShow, onReachBottom, onPageScroll } from '@dcloudio/uni-app';
import { getData } from '@/api/user.js';
import { wechatApply } from '@/api/branch/revenue.js';
const params = reactive({
	value: ''
});
const allvalue = (e) => {
	params.value = amount.value;
};
const popupRef = ref(null);
const ts = ref('');
const open = (type) => {
	if (params.value == '') {
		uni.showToast({
			icon: 'none',
			title: '请输入提现金额'
		});
		return;
	}
	if (params.value > amount.value) {
		uni.showToast({
			icon: 'none',
			title: '不能超出可提现金额'
		});
		return;
	}
	uni.showLoading({
		title: '提现申请中...'
	});
	wechatApply({
		amount: params.value
	}).then((res) => {
		if (res.code == 200 && res.data.status) {
			wx.requestMerchantTransfer({
				mchId: '1727750464', //商户号
				// subMchId: '', //子商户号，服务商模式下必填
				appId: 'wxbce0aa681a7bc714', //商户 appId
				// subAppId: '', //子商户 appId
				package: res.data.bill.package_info, //商家转账付款单跳转收款页 pkg 信息
				// openId: '', //收款用户 openId，
				success: (res) => {
					if (res.errMsg == 'requestMerchantTransfer:ok') {
						uni.showToast({
							icon: 'none',
							title: '已提现'
						});
						uni.hideLoading();
						setTimeout(() => {
							uni.navigateTo({
								url: '/mypage/more/withdrawYes?title=提现完成'
							});
						}, 500);
					}
				},
				fail: (err) => {
					uni.showToast({
						icon: 'none',
						title: err
					});
				}
			});
		} else {
			uni.hideLoading();
			ts.value = res.data.msg;
			popupRef.value.open();
		}
	});
};
const close = () => {
	popupRef.value.close();
	params.value = ''
};

const amount = ref(0);
const getInfo = () => {
	getData({}).then((res) => {
		if (res.code == 200) {
			amount.value = res.data.amount;
		}
	});
};
onLoad(() => {
	getInfo();
});
</script>
<style lang="scss">
page {
	background: #f7f8f6;
}
.popup {
	width: 620rpx;
	background: linear-gradient(180deg, #ece6d5 0%, #f7f8f6 100%);
	border-radius: 20rpx 20rpx 20rpx 20rpx;
	padding: 32rpx 30rpx;
	box-sizing: border-box;
	.icon {
		display: block;
		text-align: right;
	}
	.title {
		font-family: PingFang SC, PingFang SC;
		font-weight: bold;
		font-size: 40rpx;
		color: #4e3f23;
		text-align: center;
	}
	.pop_content {
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		color: #b19561;
		text-align: center;
		margin: 40rpx auto;
	}
	.pop_btn {
		width: 280rpx;
		height: 72rpx;
		background: #d8c6a7;
		border-radius: 40rpx 40rpx 40rpx 40rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		color: #75613e;
		text-align: center;
		line-height: 72rpx;
		margin: 0 auto;
	}
}
.top_bcg {
	background: #ece6d5;
	width: 750rpx;
	height: 140rpx;
	padding: 42rpx 60rpx;
	box-sizing: border-box;
	font-family: PingFang SC, PingFang SC;
	font-weight: 500;
	font-size: 32rpx;
	color: #1b1b1b;
}
.content {
	width: 100%;
	border-radius: 40rpx 40rpx 0rpx 0rpx;
	margin-top: -30rpx;
	background: #f7f8f6;
	padding: 44rpx 60rpx;
	box-sizing: border-box;
	.inputs {
		margin-top: 30rpx;
		padding-bottom: 24rpx;
		border-bottom: 1px solid #ededed;
		.i_l {
			font-family: 'DIN-BOLD';
			font-weight: bold;
			font-size: 40rpx;
			color: #1b1b1b;
		}
		input {
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 30rpx;
			padding-left: 10rpx;
		}
		.tt {
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #75613e;
		}
	}
	.tts {
		font-family: PingFang SC, PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		color: #8f8f8b;
		margin-top: 24rpx;
	}
}
.btn {
	width: 710rpx;
	height: 88rpx;
	background: #e3d0ad;
	border-radius: 60rpx 60rpx 60rpx 60rpx;
	font-family: PingFang SC, PingFang SC;
	font-weight: 500;
	font-size: 32rpx;
	color: #75613e;
	text-align: center;
	line-height: 88rpx;
	margin: 80rpx auto;
}
</style>
